﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CLEX.Service.DataContracts;
using CLEX.Service.ServiceContracts;

namespace CLEX.Service.Proxies
{
    public class ClientService : IClientService
    {
        private readonly string uri;

        public ClientService(string uri)
        {
            this.uri = uri;
        }

        public UserData Login(string username, string password)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.Login(username, password);
            }
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.ChangePassword(username, oldPassword, newPassword);
            }
        }

        public List<DatabaseData> GetDatabasesByUserId(int userId)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.GetDatabasesByUserId(userId);
            }
        }

        public int CreateDatabase(int userId, string connectionString)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.CreateDatabase(userId, connectionString);
            }
        }

        public void DeleteDatabase(int databaseId)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                proxy.Channel.DeleteDatabase(databaseId);
            }
        }

        public void UpdateDatabase(DatabaseData database)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                proxy.Channel.UpdateDatabase(database);
            }
        }

        public DatabaseData GetDatabase(int databaseId)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.GetDatabase(databaseId);
            }
        }

        public List<TableData> GetSchema(string connectionString)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.GetSchema(connectionString);
            }
        }

        public List<HistogramRowData> ComputeHistograms(string connectionString, QueryData queryData)
        {
            using (Proxy<IClientService> proxy = new Proxy<IClientService>(uri))
            {
                return proxy.Channel.ComputeHistograms(connectionString, queryData);
            }
        }
    }
}
